home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK1.toast / Development Kits (Disc 1) / MacTCP / MacTCP Developer Tools / HyperCard MacTCP Toolkit 1.0 / Source Code ƒ / TCPRecvChars.p < prev    next >
Encoding:
Text File  |  1994-11-21  |  2.7 KB  |  104 lines  |  [TEXT/MPS ]

  1. (*
  2.     TCPRecvChars(connectionID,count) -- Return count characters from the TCP connection.
  3.  
  4.     To compile and link this file using Macintosh Programmer's Workshop,
  5.  
  6.         pascal -w TCPRecvChars.p
  7.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=7863 -sn Main=TCPRecvChars ∂
  8.             TCPRecvChars.p.o "{Libraries}HyperXLib.o" "{MPW}"Libraries:interface.o
  9.  
  10.     © Copyright 1988 by Apple Computer, Inc.
  11.  
  12.     Initial coding 12/88 by Harry R. Chesley.
  13. *)
  14.  
  15. {$R-}
  16.  
  17. {$S TCPRecvChars }     { Segment name must be the same as the command name. }
  18.  
  19. unit DummyUnit;
  20.  
  21. interface
  22.  
  23. uses MemTypes, QuickDraw, OSIntf, HyperXCmd;
  24.  
  25. procedure EntryPoint(paramPtr: XCmdPtr);
  26.     
  27. implementation
  28.  
  29. procedure TCPRecvChars(paramPtr: XCmdPtr); forward;
  30.  
  31. procedure EntryPoint(paramPtr: XCmdPtr);
  32.  
  33.     begin
  34.         TCPRecvChars(paramPtr);
  35.     end;
  36.  
  37. procedure TCPRecvChars(paramPtr: XCmdPtr);
  38.  
  39.     var readCount: longInt;
  40.         readCountStr: Str255;
  41.         l: longInt;
  42.         p: Ptr;
  43.  
  44.     procedure Fail(errMsg: Str255); { set theResult and quit }
  45.         begin
  46.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  47.             exit(TCPRecvChars);
  48.         end;
  49.  
  50.     {$I TCPUtil.inc}
  51.  
  52.     begin
  53.         if paramPtr^.paramCount <> 2 then Fail('§§§ parameter count is not 2 §§§');
  54.  
  55.         SetUpConnectionID;
  56.  
  57.         ZeroToPas(paramPtr,paramPtr^.params[2]^,readCountStr);        { Second parameter is count to read. }
  58.         readCount := StrToNum(paramPtr,readCountStr);
  59.         if readCount < 0 then Fail('§§§ invalid count §§§');
  60.  
  61.         { Create the input handle. }
  62.         paramPtr^.returnValue := NewHandle(readCount+1);
  63.         HLock(paramPtr^.returnValue);
  64.         { Read, read, read... }
  65.         if readCount > 0 then
  66.             begin
  67.                 p := paramPtr^.returnValue^;
  68.                 { First try the internal buffer. }
  69.                 with Connection^ do
  70.                     if incomingSize > 0 then
  71.                         begin
  72.                             { Read as much as there is or as much as we need, whichever is less. }
  73.                             if readCount < incomingSize then l := readCount else l := incomingSize;
  74.                             { Copy it into the result. }
  75.                             BlockMove(incomingPtr,p,l);
  76.                             incomingPtr := Ptr(ord4(incomingPtr)+l);
  77.                             incomingSize := incomingSize-l;
  78.                             p := Ptr(ord4(p)+l);
  79.                             readCount := readCount-l;
  80.                         end;
  81.                 { If there's more needed, then read it straight from the connection. }
  82.                 if readCount > 0 then
  83.                     begin
  84.                         { Issue a read (and we'll wait until it all gets in). }
  85.                         ZeroIOParms;
  86.                         SyncControlBlock.csCode := TCPcsRcv;
  87.                         PutControlLongAtOffset(ord4(p),36);
  88.                         PutControlWordAtOffset(readCount,40);
  89.                         if PBControl(@SyncControlBlock,false) <> noErr then
  90.                             begin
  91.                                 HUnlock(paramPtr^.returnValue);
  92.                                 disposHandle(paramPtr^.returnValue);
  93.                                 Fail('§§§ receive failed §§§');
  94.                             end;
  95.                     end;
  96.             end;
  97.         { Tack on a zero termination byte. }
  98.         p := ptr(ord4(paramPtr^.returnValue^)+readCount);
  99.         p^ := 0;
  100.         HUnlock(paramPtr^.returnValue);
  101.     end;
  102.  
  103. end.
  104.